/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.netty.handler.codec.bayeux;

/**
 *
 * @author daijun
 */
public class BayeuxMessage implements BayeuxInterface {

     String channel;//Bayeux Channle Name, like /meta/handshake
     String[] supportedConnectionTypes;//A String array, like ["long-polling","http-streaming","flash","iframe"]
     String clientId;//An unify 32 chars length String for each client assigned by server
     String connectionId;//An unify 32 chars length String for each connection assigned by server
     String minimumVersion;//Support minimum version of Bayeux
     Boolean successful;//boolean
     String version;//Version of Bayeux protocal, like 1.0
     String subscription;//Subscription name of Bayeux Channel, like /chat/netty
     String error;//Error information includes error code, error args and error message, like 404:/foo/bar:Unknown Channel
     String connectionType;// Connection type, must be one of four supported connection types
     String id;//An unify 32 chars length String for each message of one application, assigned by app
     String timestamp;// ISO 8601 format in GMT time,YYYY-MM-DDThh:mm:ss.ss
     BayeuxExt ext;// Extension property for Bayeux messages
     BayeuxAdvice advice;// Advice property of some Bayeux messages
     BayeuxData data;// Data property of some Bayeux messages

    public BayeuxMessage() {
    }

    @Override
    public String toJSON() {
        StringBuilder json = new StringBuilder();
        json.append("[{");
        if (this.channel != null && this.channel.length() != 0) {
            json.append("\"channel\":").append(JSONParser.toJSON(this.channel));
        }
        if (supportedConnectionTypes != null && supportedConnectionTypes.length != 0) {
            json.append(",\"supportedConnectionTypes\":").append(JSONParser.toJSON(supportedConnectionTypes));
        }
        if (clientId != null && clientId.length() != 0) {
            json.append(",\"clientId\":").append(JSONParser.toJSON(clientId));
        }
        if (connectionId != null && connectionId.length() != 0) {
            json.append(",\"connectionId\":").append(JSONParser.toJSON(connectionId));
        }
        if (minimumVersion != null && minimumVersion.length() != 0) {
            json.append(",\"minimumVersion\":").append(JSONParser.toJSON(minimumVersion));
        }
        if (successful != null) {
            json.append(",\"successful\":").append(JSONParser.toJSON(successful));
        }
        if (version != null && version.length() != 0) {
            json.append(",\"version\":").append(JSONParser.toJSON(version));
        }
        if (subscription != null && subscription.length() != 0) {
            json.append(",\"subscription\":").append(JSONParser.toJSON(subscription));
        }
        if (error != null && error.length() != 0) {
            json.append(",\"error\":").append(JSONParser.toJSON(error));
        }
        if (connectionType != null && connectionType.length() != 0) {
            json.append(",\"connectionType\":").append(JSONParser.toJSON(connectionType));
        }
        if (id != null && id.length() != 0) {
            json.append(",\"id\":").append(JSONParser.toJSON(id));
        }
        if (timestamp != null && timestamp.length() != 0) {
            json.append(",\"timestamp\":").append(JSONParser.toJSON(timestamp));
        }
        if (ext != null) {
            json.append(",\"ext\":" + ext.toJSON());
        }
        if (advice != null) {
            json.append(",\"advice\":" + advice.toJSON());
        }
        if (data != null) {
            json.append(",\"data\":" + data.toJSON());
        }
        json.append("}]");
        return json.toString();
    }

    @Override
    public boolean isValid() {
        return (channel == null || channel.length() == 0);
    }

    public BayeuxAdvice getAdvice() {
        return advice;
    }

    public void setAdvice(BayeuxAdvice advice) {
        this.advice = advice;
    }

    public String getChannel() {
        return channel;
    }

    public void setChannel(String channel) {
        this.channel = channel;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getConnectionId() {
        return connectionId;
    }

    public void setConnectionId(String connectionId) {
        this.connectionId = connectionId;
    }

    public String getConnectionType() {
        return connectionType;
    }

    public void setConnectionType(String connectionType) {
        this.connectionType = connectionType;
    }

    public BayeuxData getData() {
        return data;
    }

    public void setData(BayeuxData data) {
        this.data = data;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public BayeuxExt getExt() {
        return ext;
    }

    public void setExt(BayeuxExt ext) {
        this.ext = ext;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSubscription() {
        return subscription;
    }

    public void setSubscription(String subscription) {
        this.subscription = subscription;
    }

    public Boolean getSuccessful() {
        return successful;
    }

    public void setSuccessful(Boolean successful) {
        this.successful = successful;
    }

    public String[] getSupportedConnectionTypes() {
        return supportedConnectionTypes;
    }

    public void setSupportedConnectionTypes(String[] supportedConnectionTypes) {
        this.supportedConnectionTypes = supportedConnectionTypes;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getMinimumVersion() {
        return minimumVersion;
    }

    public void setMinimumVersion(String minimumVersion) {
        this.minimumVersion = minimumVersion;
    }
}
